#region The MIT License
// The MIT License
// 
// Copyright (c) 2011 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.IO;
using System.Xml;
using octalforty.Staytuned.ObjectModel;

namespace octalforty.Staytuned.Impl.ComponentModel.Fs
{
    /// <summary>
    /// An implementation of <see cref="IFsTemplateReader"/> which reads <see cref="Template"/> objects from XML files.
    /// </summary>
    /// <remarks>
    /// Format is as follows: 
    /// <code>
    /// &lt;template>
    ///     &lt;subject>&lt;/subject>
    ///     &lt;body>&lt;/body>
    /// &lt;/template>
    /// </code>
    /// Optionally, template file may contain <c>&lt;shared-prefix></c> element, content of
    /// which is prepended both to <c>body </c>and to <c>subject</c>:
    /// <code>
    /// &lt;template>
    ///     &lt;subject>&lt;/subject>
    ///     &lt;body>&lt;/body>
    ///     &lt;shared-prefix>&lt;/shared-prefix>
    /// &lt;/template>
    /// </code>
    /// <para />
    /// <see cref="FsXmlTemplateReader"/> automatically adds <c>.xml</c> extension to the <c>path</c> provided.
    /// </remarks>
    public class FsXmlTemplateReader : IFsTemplateReader
    {
        public bool TemplateExists(string path)
        {
            if(!path.ToLowerInvariant().EndsWith(".xml"))
                path += ".xml";

            return File.Exists(path);
        }

        public Template ReadTemplate(string path)
        {
            if(!path.ToLowerInvariant().EndsWith(".xml"))
                path += ".xml";

            var xmlDocument = new XmlDocument();
            xmlDocument.Load(path);

            var subjectNode = xmlDocument.SelectSingleNode("template/subject");
            var bodyNode = xmlDocument.SelectSingleNode("template/body");
            var sharedPrefixNode = xmlDocument.SelectSingleNode("template/shared-prefix");

            var sharedPrefix = sharedPrefixNode == null ?
                "" :
                sharedPrefixNode.InnerText;

            return new Template(sharedPrefix + subjectNode.InnerText, sharedPrefix + bodyNode.InnerText);
        }
    }
}